//背景图片
const router = require('koa-router')()
const fs = require("fs");
const path = require("path");
const uploadDir = path.join(__dirname, '../images/bg');
router.prefix('/background')
//background
//编辑->上传新的图片
router.post('/uploadimg', async (ctx, next) => {
  let bgid = ctx.request.body.bgid;
  let bgweather = ctx.request.body.bgweather;
  let bgplace = ctx.request.body.bgplace;
  let bgimg = ctx.request.body.bgimg;
  // console.log(ctx.request.body);

  let base64 = bgimg;
  let base64Data = base64.replace(/^data:image\/\w+;base64,/, "");
  let dataBuffer = Buffer.from(base64Data, 'base64');
  // console.log(dataBuffer);
  let name = Date.now() + Math.floor(Math.random()) + '.jpg'
  let imgUrl = uploadDir + '\\' + name
  fs.writeFileSync(imgUrl, dataBuffer)
  let result0 = await ctx.db.EXCUTE('select bgimg from background where bgid = ?', [bgid])
  fs.unlink(path.join(__dirname, '../' + result0[0].bgimg), async function (err) {
    if (err) {
      console.log(err);
      ctx.body = {
        code: 500
      }
    }
  })
  let sql = 'UPDATE background SET bgimg=?, bgweather=?, bgplace=? WHERE bgid=?'
  let result1 = await ctx.db.EXCUTE(sql, ['/images/bg/' + name, bgweather, bgplace, bgid]);

  let result = await ctx.db.EXCUTE('SELECT * FROM background ORDER BY bgid');

  ctx.body = {
    code: 200,
    data: result
  }

});
//不上传新的图片
router.post('/uploadnoimg', async (ctx, next) => {
  let bgid = ctx.request.body.bgid;
  let bgweather = ctx.request.body.bgweather;
  let bgplace = ctx.request.body.bgplace;

  let sql = 'UPDATE background SET  bgweather=?, bgplace=? WHERE bgid=?'
  let result1 = await ctx.db.EXCUTE(sql, [bgweather, bgplace, bgid]);

  let result = await ctx.db.EXCUTE('SELECT * FROM background ORDER BY bgid');

  ctx.body = {
    code: 200,
    data: result
  }

});

//页面初始化时获取数据库内的数据条目
router.post('/getbackground', async (ctx, next) => {
  let result = await ctx.db.EXCUTE('SELECT * FROM background ORDER BY bgid');
  ctx.body = {
    code: 200,
    data: result
  }
})

//编辑item 首先获取该项的内容
router.post('/getsingle', async (ctx, next) => {
  let bgid = ctx.request.body.bgid;

  let sql = 'SELECT * FROM background WHERE bgid=?';
  let result = await ctx.db.EXCUTE(sql, [bgid]);

  ctx.body = {
    code: 200,
    data: result
  }
})

//删除项backend
router.post('/delbgbackend', async (ctx, next) => {
  let bgid = ctx.request.body.bgid;

  let sql = 'DELETE FROM background WHERE bgid=?';
  await ctx.db.EXCUTE(sql, [bgid]);

  let result = await ctx.db.EXCUTE('SELECT * FROM background ORDER BY bgid');
  ctx.body = {
    code: 200,
    data: result
  }
})

router.post('/getbg', async (ctx, next) => {
  let bgweather = ctx.request.body.bgweather;

  let sql = 'SELECT * FROM background WHERE bgweather=? and bgplace';
  let result = await ctx.db.EXCUTE(sql, [bgweather]);

  ctx.body = {
    code: 200,
    data: result
  }
})

// 根据bgplace获取用户选择的背景
router.post('/getselbg', async (ctx, next) => {
  let bgweather = ctx.request.body.bgweather;
  let bgplace = ctx.request.body.bgplace;

  console.log(ctx.request.body);
  let sql = 'SELECT * FROM background WHERE bgweather=? and bgplace=?';
  let result = await ctx.db.EXCUTE(sql, [bgweather, bgplace]);
  console.log(result);
  ctx.body = {
    code: 200,
    data: result
  }
})


module.exports = router
